package com.greenhouse9.prog01;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.OutputStream;

public class Prog1 {

	private File f1;

	public Prog1(){};
	public Prog1(String filename){
		f1 = new File(filename);
	}

	public void printFileInfo(){

		System.out.println("file name: "   + f1.getName() );
		System.out.println("file path: "      + f1.getPath() );
		System.out.println("absolute path: " + f1.getAbsolutePath() );
		System.out.println("parent path: "   + f1.getParent());

		try {
			System.out.println("canonical path: " + f1.getCanonicalPath() );
		}
		catch (IOException e){
			e.printStackTrace();
		}

		if (f1.isFile()) {
			System.out.println(f1.getName() +" is file.");
		} else if (f1.isDirectory()){
			System.out.println(f1.getName() +" is directory");
		}

		if (f1.canRead()){
			System.out.println(f1.getName() + " is readable.");
		}

		if (f1.canWrite()){
			System.out.println(f1.getName() + " is writable.");
		}

		System.out.println("file size: " + f1.length());

		if (f1.isDirectory()){
			String [] filelist;
			filelist = f1.list();
			for (int i=0; i< filelist.length; i++){
				System.out.println(filelist[i]);
			}
		}
	}

	public void checkFileExists (){

		if(!f1.exists()){
			System.out.println(f1.getName() + "がありません");
			return;
		}
	}

	public void outputData(){
  		outputData(0, System.out);
	}

	public void outputData(int n){
		outputData(n, System.out);
	}

	public void outputData(OutputStream out){
		outputData(0, out);
	}

	public void outputData(int n, OutputStream out){

		if(!f1.isFile())return;

		if(out==null){
			return;
		}

		// size of output
		int sizeCounter = 0;

		InputStream in = null;
		try {
			in = new FileInputStream(f1);
  			int ch;
  			while ((ch = in.read()) != -1){
  				if (n>0 && sizeCounter++ >= n){
  					break;
  				}
  				ch = ch ^ 1;
  				ch = ch ^ 1;
  				out.write(ch);
  			}
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		finally {
			try {
				in.close();
				//out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public void outputData(int off, int n){
		outputData(System.out, off, n);
	}
	
	public void outputData(OutputStream out, int off, int n){
		
		if(!f1.isFile())return;

		if(out==null){
			return;
		}

		// size of output
		int sizeCounter = 0;
		
		InputStream in = null;
		try {
			in = new FileInputStream(f1);
  			int ch;
  			
  			//skip
  			in.skip(off);
  			
  			while ((ch = in.read()) != -1){
  				out.write(ch);
  				sizeCounter = sizeCounter + 1;
  				if (n>0 && sizeCounter >= n){
  					break;
  				}
  			}
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		finally {
			try {
				in.close();
				//out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	public void outputData(String outputfile, int off, int len){
		File f = new File(outputfile);
		
		try {
			FileOutputStream fos = new FileOutputStream (f);
			outputData(fos, off, len);
		}
		catch(FileNotFoundException e){
			return;
		}
	}
	
	public long getFileSize(){
		return f1.length();
	}
	
	public File getF1() {
		return f1;
	}

	public void setF1(File f1) {
		this.f1 = f1;
	}
}
